package org.jeecg.modules.monitoring.j_monitoring.gatewayData;

import com.alibaba.fastjson.JSONArray;
import com.xkcoding.http.util.StringUtil;
import org.jeecg.common.util.CommonUtils;
import org.jeecg.config.sign.util.NoSslHttpUtils;
import org.jeecg.config.vo.HttpResult;
import org.jeecg.modules.monitoring.j_monitoring.entity.fdnEntity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 计算资源获取数据
 */
@Component
public class ComputeResources {
    @Autowired
    private Environment env;

    private String params = "CPUMonitor,MemoryMonitor,DiskMonitor,TotalProcessesMonitor";

    private String deviceMonitors = "/ng/integration/deviceMonitors";

    private String windowsJsonFilePath = "D:\\jsonFile\\windows.txt";

    private String linuxJsonFilePath = "D:\\jsonFile\\linux.txt";

    //处理是虚拟机主机 还是 agent
    public ComputeResultData getData(String fdn,String way){

        ComputeResultData resultData = new ComputeResultData();

        String url = env.getProperty("gatewaySystem.url");

        String header = env.getProperty("gatewaySystem.header");

        String path = fdn.indexOf("Linux")!=-1?this.linuxJsonFilePath:this.windowsJsonFilePath;

        String queryUrl = url + deviceMonitors + "?device_fdns=" + fdn ;

        String json = "";
        if (!"prod".equals(env.getProperty("spring.profiles.active"))) {
            File jsonFile = new File(path);
            json = CommonUtils.getStr(jsonFile);
        } else {
            HttpResult res = NoSslHttpUtils.noSslGet(queryUrl, header);
            json = res.getBody();
        }

        if (StringUtil.isEmpty(way) || "Agent".equals(way)){
            resultData = this.getAgentData(json);
        }else {
            resultData =this.getHostData(json);
        }
        return resultData;
    }


    public ComputeResultData getAgentData(String json){
        ComputeResultData resultData = new ComputeResultData();
        try {
            List<DevicesMonitors> monitorsList = JSONArray.parseArray(json, DevicesMonitors.class);
            DevicesMonitors monitors = monitorsList.get(0);

            List<StorageDistItem> distItems = new ArrayList<>();
            for (DevicesCounters counters :monitors.getMonitors()){


                if ("CPUMonitor".equals(counters.getType()) && counters.getCounters().size()>0){
                    for (DevicesData devicesData :counters.getCounters()){
                        if ("Total CPU Utilization".equals(devicesData.getName())){
                            resultData.setCpu(devicesData.getValue() + devicesData.getUnit());
                        }
                    }
                }

                if ("MemoryMonitor".equals(counters.getType()) && counters.getCounters().size()>0){
                    for (DevicesData devicesData :counters.getCounters()){
                        if ("Memory Utilization".equals(devicesData.getName())){
                            resultData.setMemoryOccupy(devicesData.getValue() + devicesData.getUnit());
                        }
                    }
                }

                if ("TotalProcessesMonitor".equals(counters.getType()) && counters.getCounters().size()>0){
                    for (DevicesData devicesData :counters.getCounters()){
                        if ("COUNT".equals(devicesData.getName())){
                            resultData.setProcessesTotal(devicesData.getValue());
                        }
                    }
                }


                if ("DiskMonitor".equals(counters.getType())){
                    StorageDistItem distItem = new StorageDistItem();
                    //磁盘监控
                    for (DevicesData devicesData :counters.getCounters()){
                        if ("Disk Utilization".equals(devicesData.getName())){
                            distItem.setTotalPercentage(devicesData.getValue() + devicesData.getUnit());
                        }
                    }
                    distItem.setDistName(counters.getName());
                    distItems.add(distItem);
                }


            }
            resultData.setDistItemList(distItems);

        }catch (Exception e){
            e.printStackTrace();
        }

        return resultData;
    }


    public ComputeResultData getHostData(String json){
        ComputeResultData resultData = new ComputeResultData();

        try {
            List<DevicesMonitors> monitorsList = JSONArray.parseArray(json, DevicesMonitors.class);
            DevicesMonitors monitors = monitorsList.get(0);

            List<StorageDistItem> distItems = new ArrayList<>();
            for (DevicesCounters counters :monitors.getMonitors()){


                if ("VmwareHostCpu".equals(counters.getType()) && counters.getCounters().size()>0){
                    for (DevicesData devicesData :counters.getCounters()){
                        if ("Utilization".equals(devicesData.getName())){
                            resultData.setCpu(devicesData.getValue() + devicesData.getUnit());
                        }
                    }
                }

                if ("VmwareHostMemory".equals(counters.getType()) && counters.getCounters().size()>0){
                    for (DevicesData devicesData :counters.getCounters()){
                        if ("Utilization".equals(devicesData.getName())){
                            resultData.setMemoryOccupy(devicesData.getValue() + devicesData.getUnit());
                        }
                    }
                }

                if ("TotalProcessesMonitor".equals(counters.getType()) && counters.getCounters().size()>0){
                    for (DevicesData devicesData :counters.getCounters()){
                        if ("COUNT".equals(devicesData.getName())){
                            resultData.setProcessesTotal(devicesData.getValue());
                        }
                    }
                }


                if ("VmwareHostScsiLun".equals(counters.getType())){
                    StorageDistItem distItem = new StorageDistItem();
                    //磁盘监控
                    for (DevicesData devicesData :counters.getCounters()){
                        if ("Disk Utilization".equals(devicesData.getName())){
                            distItem.setTotalPercentage(devicesData.getValue() + devicesData.getUnit());
                        }
                    }
                    distItem.setDistName(counters.getName());
                    distItems.add(distItem);
                }


            }
            resultData.setDistItemList(distItems);

        }catch (Exception e){
            e.printStackTrace();
        }


        return resultData;
    }


}
